#!/bin/sh
# GitLab pre-install script

DEST_DIR=<%= install_dir %>

mkdir -p /var/log/gitlab/reconfigure

skip_migrations_file=/etc/gitlab/skip-auto-migrations

main() {
  if [ -e "${skip_migrations_file}" ] ; then
    # The user wants us to do nothing
    return
  fi

  if [ -d ${DEST_DIR}/service/unicorn ] ; then
    notify "Automatically backing up only the GitLab SQL database (excluding everything else!)"
    if ! ${DEST_DIR}/bin/gitlab-rake gitlab:backup:create SKIP=repositories,uploads,builds,artifacts,lfs,registry ; then
      notify
      notify "Backup failed! If you want to skip this backup, run the following command and"
      notify "try again:"
      notify
      notify "  sudo touch ${skip_migrations_file}"
      notify
      exit 1
    fi
  fi
}

notify() {
  echo "gitlab preinstall: $1"
}

if [ -n "${GITLAB_DEBUG}" ] ; then
  notify "debug: arguments: $@"
fi

case "$1" in
  2)
    # Looks like an RPM upgrade
    main
    ;;
  upgrade)
    # Looks like a DEB upgrade
    main
    ;;
  *)
    # This is not an upgrade, nothing to do.
    ;;
esac
